package com.atguigu.gmall.item.service.impl;

import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 商品详情页展示接口实现类
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    /**
     * 查询商品详情页需要的所有数据
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getSkuInfo(Long skuId) {
        //初始化返回参数
        Map result = new ConcurrentHashMap();
        //参数校验
        if (skuId == null){
            return null;
        }
        //返回商品详情SkuInfo
        CompletableFuture<SkuInfo> future1 = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeign.getSkuInfo(skuId);
            if (skuInfo == null || skuInfo.getId() == null) {
                return null;
            }
            result.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);


        //返回商品图片 skuImage
        CompletableFuture<Void> future2 = future1.thenAcceptAsync((skuInfo) -> {
            if (skuInfo == null || skuInfo.getId() == null) {
                return ;
            }
            List<SkuImage> skuImage = productFeign.getSkuImage(skuInfo.getId());
            //skuInfo.setSkuImageList(skuImage);
            result.put("skuImage", skuImage);
        },threadPoolExecutor);

        //查询商品分类
        CompletableFuture<Void> future3 = future1.thenAcceptAsync((skuInfo) -> {
            if (skuInfo == null || skuInfo.getId() == null) {
                return ;
            }
            Long category3Id = skuInfo.getCategory3Id();
            BaseCategoryView category = productFeign.getCategory(category3Id);
            result.put("category", category);
        },threadPoolExecutor);

        //查询商品价格
        CompletableFuture<Void> future4 = future1.thenAcceptAsync((skuInfo) -> {
            if (skuInfo == null || skuInfo.getId() == null) {
                return ;
            }
            BigDecimal price = productFeign.getPrice(skuInfo.getId());
            result.put("price", price);
        },threadPoolExecutor);
        //销售属性值
        CompletableFuture<Void> future5 = future1.thenAcceptAsync((skuInfo) -> {
            if (skuInfo == null || skuInfo.getId() == null) {
                return ;
            }
            List<SpuSaleAttr> spuSaleAttr = productFeign.getSpuSaleAttr(skuInfo.getId(), skuInfo.getSpuId());
            result.put("spuSaleAttr", spuSaleAttr);
        });


        //根据指定的spu的id查询这个spu所拥有的全部sku的id和每个sku对应的销售属性值的内容
        CompletableFuture<Void> future6 = future1.thenAcceptAsync((skuInfo) -> {
            if (skuInfo == null || skuInfo.getId() == null) {
                return ;
            }
            Map skuValuesBySpu = productFeign.getSkuValuesBySpu(skuInfo.getSpuId());
            result.put("skuValuesBySpu", skuValuesBySpu);

        },threadPoolExecutor);
       CompletableFuture.allOf(future1, future2, future3, future4, future5, future6).join();
        return result;

    }
}
